package com.jh.fcsm.controller.sys;

import com.github.pagehelper.PageInfo;
import com.jh.fcsm.beans.sys.SysUser;
import com.jh.fcsm.beans.sys.vo.SysUserVo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.common.RestApiResponse;
import com.jh.fcsm.common.annotation.OperLogAnnotation;
import com.jh.fcsm.common.annotation.RepeatSubAnnotation;
import com.jh.fcsm.common.annotation.SystemLogAnnotation;
import com.jh.fcsm.enums.UserTypeEnum;
import com.jh.fcsm.service.sys.SysUserService;
import com.jh.fcsm.util.AppUserUtil;
import com.jh.fcsm.util.poi.EasyExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户 controller
 *
 * @author szx
 * @date 2020-07-05 16:25:12
 */
@RestController
@RequestMapping("/sys/user")
@Api(value = "用户接口", tags = "用户接口")
public class SysUserController extends BaseController {

    private static final String PER_PREFIX = "btn:sys:user:";

    @Autowired
    private SysUserService sysUserService;

    /**
     * 去绑定 google 动态码
     *
     * @param sysUser 用户信息
     * @return 用户ID
     */
    @RepeatSubAnnotation
    @PostMapping("/bindSer")
    @ApiOperation(value = "去绑定 google 动态码", notes = "去绑定 google 动态码")
    @SystemLogAnnotation(type = "去绑定 google 动态码", value = "去绑定 google 动态码")
    public RestApiResponse<?> bindSer(@RequestBody SysUserVo sysUser, HttpServletRequest request) {
        String ip=getCurrentIp(request);
        return RestApiResponse.ok(sysUserService.bindSer(sysUser,ip));
    }

    /**
     * 确认绑定 google 动态码
     *
     * @param sysUser 用户信息
     * @return 用户ID
     */
    @RepeatSubAnnotation
    @PostMapping("/qrbindSer")
    @ApiOperation(value = "确认绑定 google 动态码", notes = "确认绑定 google 动态码")
    @SystemLogAnnotation(type = "确认绑定 google 动态码", value = "确认绑定 google 动态码")
    public RestApiResponse<?> qrbindSer(@RequestBody SysUserVo sysUser, HttpServletRequest request) {
        String ip=getCurrentIp(request);
        sysUserService.qrbindSer(sysUser,ip);
        return RestApiResponse.ok();
    }


    /**
     * 新增用户
     *
     * @param sysUser 用户信息
     * @return 用户ID
     */
    @RepeatSubAnnotation
    @PostMapping("/uppwd")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @SystemLogAnnotation(type = "修改密码", value = "修改密码")
    public RestApiResponse<?> uppwd(@RequestBody SysUserVo sysUser, HttpServletRequest request) {
        String ip=getCurrentIp(request);
        sysUserService.uppwd(sysUser,ip);
        return RestApiResponse.ok();
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户
     */
    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/getCurrentUser")
    public RestApiResponse<?> getCurrentUser() {
        return RestApiResponse.ok(AppUserUtil.getLoginAppUser());
    }

    /**
     * 新增用户
     *
     * @param sysUser 用户信息
     * @return 用户ID
     */
    @RepeatSubAnnotation
    @PostMapping("/save")
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @SystemLogAnnotation(type = "用户管理", value = "新增用户")
    @OperLogAnnotation(value = "'新增用户, 用户名:'+#sysUser.username", module = "系统管理", submodule = "用户管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "save')")
    public RestApiResponse<?> saveSysUser(@RequestBody SysUserVo sysUser) {
        // 非管理员禁止添加管理员
        if (!isAdmin()) {
            sysUser.setType(UserTypeEnum.ORDINARY.toString());
        }
        String id = sysUserService.saveOrUpdate(sysUser);
        return RestApiResponse.ok(id);
    }

    /**
     * 用户管理
     *
     * @param sysUser 用户信息
     * @return 用户ID
     */
    @RepeatSubAnnotation
    @PostMapping("/update")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @SystemLogAnnotation(type = "用户管理", value = "修改用户")
    @OperLogAnnotation(value = "'修改用户, 用户名:'+#sysUser.username", module = "系统管理", submodule = "用户管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "update')")
    public RestApiResponse<?> updateSysUser(@RequestBody SysUserVo sysUser) {
        // 非管理员禁止添加管理员角色
        if (!isAdmin()) {
            sysUser.setType(UserTypeEnum.ORDINARY.toString());
        }
        String id = sysUserService.saveOrUpdate(sysUser);
        return RestApiResponse.ok(id);
    }

    /**
     * 删除用户
     *
     * @param ids 用户IDs
     * @return 结果
     */
    @RepeatSubAnnotation
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @SystemLogAnnotation(type = "用户管理", value = "删除用户")
    @OperLogAnnotation(value = "'删除用户, 用户名Ids:'+#ids", module = "系统管理", submodule = "用户管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "delete')")
    public RestApiResponse<?> deleteSysUser(@RequestBody List<String> ids) {
        sysUserService.deleteSysUser(ids);
        return RestApiResponse.ok();
    }

    /**
     * 获取用户详情
     *
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/findById")
    @ApiOperation(value = "获取用户详情", notes = "获取用户详情")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "find')")
    public RestApiResponse<?> findById(@RequestParam("id") String id) {
        SysUser sysUser = sysUserService.findById(id);
        return RestApiResponse.ok(sysUser);
    }

    /**
     * 分页查询用户
     *
     * @param sysUserVo 查询列表
     * @return 结果
     */
    @PostMapping("/findPageByQuery")
    @ApiOperation(value = "分页查询用户", notes = "分页查询用户")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "query')")
    public RestApiResponse<?> findPageByQuery(@RequestBody SysUserVo sysUserVo) {
        PageInfo<SysUserVo> sysUser = sysUserService.findPageByQuery(sysUserVo);
        return RestApiResponse.ok(sysUser);
    }

    /**
     * 修改密码
     *
     * @param sysUserVo 修改密码
     * @return RestApiResponse<?>
     */
    @PostMapping("/updatePwd")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @SystemLogAnnotation(type = "用户管理", value = "修改密码")
    @OperLogAnnotation(value = "'修改密码, 用户名Id:'+#sysUserVo.id", module = "系统管理", submodule = "用户管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "updatePwd')")
    public RestApiResponse<?> updatePassword(@RequestBody SysUserVo sysUserVo) {
        sysUserService.updatePassword(sysUserVo);
        return RestApiResponse.ok();
    }

    /**
     * 修改用户密码
     *
     * @param sysUserVo 查询条件
     * @return 结果
     */
    @PostMapping("/resetPassword")
    @ApiOperation(value = "通过主键修改对应用户得密码", notes = "通过主键修改对应用户得密码")
    @SystemLogAnnotation(type = "用户管理", value = "修改密码")
    @OperLogAnnotation(value = "'修改密码, 用户名Id:'+#sysUserVo.id", module = "系统管理", submodule = "用户管理")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "updatePrimaryPassword')")
    public RestApiResponse<?> resetPassword(@RequestBody SysUserVo sysUserVo) {
        sysUserService.resetPassword(sysUserVo);
        return RestApiResponse.ok();
    }

    /**
     * 导出用户
     *
     * @param sysUserVo 查询条件
     */
    @PostMapping("/exportExcel")
    @ApiOperation(value = "导出用户", notes = "导出用户")
    @PreAuthorize("hasAuthority('" + PER_PREFIX + "export')")
    public void exportExcel(SysUserVo sysUserVo) {
        sysUserVo.setPageNum(99999);
        PageInfo<SysUserVo> sysUser = sysUserService.findPageByQuery(sysUserVo);
        EasyExcelUtils.exportExcel(sysUser.getList(), "系统用户", SysUserVo.class, "系统用户.xlsx", response);
    }

    /**
     * 判断用户名是否已存在
     *
     * @param username 用户名
     * @return 结果
     */
    @GetMapping("/getUserNameIsRepeatable")
    @ApiOperation(value = "用户名是否重复", notes = "用户名是否重复")
    public RestApiResponse<?> getUserNameIsRepeatable(@RequestParam("username") String username) {
        sysUserService.existsUserByUsername(username);
        return RestApiResponse.ok();
    }

}
